home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / CantTouchThis / AEGetStuff.c next >
Encoding:
Text File  |  1995-06-24  |  8.1 KB  |  219 lines  |  [TEXT/KAHL]

  1. // AEGetStuff.c
  2.  
  3. #include <AppleEvents.h>
  4. #include <AEObjects.h>
  5. #include <AERegistry.h>
  6. #include <AEPackObject.h>
  7. #include <Aliases.h>
  8. #include <Processes.h>
  9.  
  10. #include "ScriptableFinder.h"
  11.  
  12. #ifndef TRUE
  13.     #define FALSE 0
  14.     #define TRUE (!FALSE)
  15. #endif
  16.  
  17. #include <Packages.h>
  18.  
  19. #define procNotFound -1    // tmp - can't find
  20.  
  21. Boolean    IsRelativeProcessSerialNumber (ProcessSerialNumber *psn)
  22.  
  23. /*    A simple routine which determines whether the process serial number given is a relative one.
  24.     This means that it isn't a number which refers to the process outright but rather refers to the
  25.     process because it's current.
  26.  
  27.      Input: psn - the ProcessSerialNumber to check.
  28.  
  29.     Output: Boolean result. */
  30.  
  31. {
  32.     return((psn->highLongOfPSN == 0) && (psn->lowLongOfPSN == kCurrentProcess));
  33. }
  34.  
  35.  
  36. //**************************************************************************
  37. OSErr    GetFinderProcess (ProcessSerialNumber *finderpsn, Boolean shortcut);
  38.  
  39.     // replace LDR's export routine
  40.  
  41. OSErr    GetFinderProcess (ProcessSerialNumber *finderpsn, Boolean shortcut)
  42.  
  43. /*    A routine to determine the process serial number of the Finder. It returns the result as a
  44.     parameter passed by reference. It also has a Boolean parameter which specifies whether or not
  45.     the returned process serial number will represent the Finder process serial number as
  46.     "kCurrentProcess" if it is the current process to allow a shortcut of the _GetNextEvent call
  47.     dependencies of processing an AppleEvent.
  48.  
  49.      Input:    shortcut - allow "kCurrentProcess" to be used if we are in the current process.
  50.      Input:    *finderpsn - result ProcessSerialNumber passed by reference.
  51.  
  52.     Output:    error code that occured. */
  53.  
  54. {
  55.     Boolean                result;
  56.     ProcessSerialNumber    psn, currentpsn;
  57.     ProcessInfoRec        pir;
  58.  
  59.     psn.highLongOfPSN = 0;
  60.     psn.lowLongOfPSN = kNoProcess;
  61.     pir.processInfoLength = sizeof(ProcessInfoRec);
  62.     pir.processName = nil;            // don't want these bits of information
  63.     pir.processAppSpec = nil;
  64.     while (GetNextProcess(&psn) == noErr) 
  65.         if (GetProcessInformation(&psn, &pir) == noErr) 
  66.             if ((pir.processType == kFinderType) && (pir.processSignature == kFinderSignature)) 
  67.             {
  68.                 if (shortcut && (GetCurrentProcess(¤tpsn) == noErr) && 
  69.                     (SameProcess(¤tpsn, &psn, &result) == noErr) && result) 
  70.                 {            // use the current process to shortcut the event dispatching
  71.                     finderpsn->highLongOfPSN = 0;
  72.                     finderpsn->lowLongOfPSN = kCurrentProcess;
  73.                 }
  74.                 else
  75.                     *finderpsn = psn;            // found the Finder's psn
  76.                 return(noErr);            // got the process serial number
  77.             }
  78.     return(procNotFound);            // got an error - not found
  79. }
  80.  
  81. //**************************************************************************
  82.  
  83. OSErr    GetScriptableFinderFilePos (FSSpec *f, Boolean usesystemmode, AESendMode sendmode, Boolean sendToSelf, Rect *r);
  84. OSErr    GetScriptableFinderFilePos (FSSpec *f, Boolean usesystemmode, AESendMode sendmode, Boolean sendToSelf, Rect *r)
  85.  
  86. /*    
  87.  
  88.      Input:    f - FSSpec which contains the item to the position of.
  89.      Input:    usesystemmode - a Boolean which determines whether the system's PPC port will be used.
  90.      Input: sendmode - which mode to send the Apple Event to the Finder.
  91.  
  92.     Output:    error code that occured. */
  93.  
  94. {
  95.     short                errnum;
  96.     OSType                icondescriptor, finderSig = 'MACS';
  97.     ProcessSerialNumber    targetpsn;
  98.     AEDesc                targetaddress, propdesc, filedesc, directdesc, nulldesc, targetdesc, pointdesc;
  99.     AppleEvent            myae, myReply;
  100.     Rect                rect = {0,0,0,0};
  101.  
  102.  
  103.     nulldesc.descriptorType = typeNull;
  104.     nulldesc.dataHandle = nil;
  105.  
  106.     icondescriptor = pBounds;
  107.     if (GetFinderProcess(&targetpsn, sendToSelf) == noErr) 
  108.     {
  109.         if (usesystemmode)     TurnSystemModeOn();            // you really DON'T want to do this
  110.         
  111.         if ((errnum = AECreateDesc(typeProcessSerialNumber, &targetpsn, sizeof(ProcessSerialNumber), &targetaddress)) == noErr) {
  112.             if ((errnum = AECreateAppleEvent(kAECoreSuite, kAEGetData, &targetaddress, kAutoGenerateReturnID, kAnyTransactionID, &myae)) == noErr) {
  113.                 if ((errnum = AECreateDesc(typeType, &icondescriptor, sizeof(DescType), &propdesc)) == noErr) {
  114.                     AliasHandle    targetAlias;
  115.                     NewAlias(nil, f, &targetAlias);
  116.                     HLock((Handle)targetAlias);
  117.                     if ((errnum = AECreateDesc(typeAlias, *targetAlias, GetHandleSize((Handle)targetAlias), &filedesc)) == noErr) {
  118.                         if ((errnum = CreateObjSpecifier(typeWildCard, &nulldesc, typeAlias,&filedesc, TRUE, &targetdesc)) == noErr) {
  119.                             if ((errnum = CreateObjSpecifier(cProperty, &targetdesc, formPropertyID, &propdesc, TRUE, &directdesc)) == noErr) {
  120.                                 if ((errnum = AEPutParamDesc(&myae, keyDirectObject, &directdesc)) == noErr) {
  121.                                     errnum = AESend(&myae, &myReply, sendmode, kAENeverInteract, kAEDefaultTimeout, nil, nil);
  122.                                     if ( noErr) {
  123.                                         // Get Rect
  124.                                         if (((errnum = AEGetParamDesc(&myReply, keyAEResult, typeQDPoint, &pointdesc)) == noErr))
  125.                                             rect = **((Rect **)(pointdesc.dataHandle));
  126.                                     }
  127.                                 }
  128.                                 
  129.                                 AEDisposeDesc(&directdesc);
  130.                             }
  131.                         }
  132.                         AEDisposeDesc(&filedesc);
  133.                     }
  134.                     HUnlock((Handle)targetAlias);
  135.                     AEDisposeDesc(&propdesc);
  136.                 }
  137.                 AEDisposeDesc(&myae);
  138.             }
  139.             AEDisposeDesc(&targetaddress);
  140.         }
  141.         
  142.         // Get Rect
  143.         *r = rect;
  144.         
  145.         if (usesystemmode)     TurnSystemModeOff();            // we really DIDN'T want to do this
  146.     }
  147.     return(errnum);            // the result
  148. }
  149.  
  150.  
  151. //---------------------------------------------------------------------------------------------
  152. OSErr    GetScriptableFinderTrashPos (Boolean usesystemmode, AESendMode sendmode, Boolean sendToSelf, Rect *r);
  153. OSErr    GetScriptableFinderTrashPos (Boolean usesystemmode, AESendMode sendmode, Boolean sendToSelf, Rect *r)
  154.  
  155. /*    
  156.  
  157.      Input:    f - FSSpec which contains the item to the position of.
  158.      Input:    usesystemmode - a Boolean which determines whether the system's PPC port will be used.
  159.      Input: sendmode - which mode to send the Apple Event to the Finder.
  160.  
  161.     Output:    error code that occured. */
  162.  
  163. {
  164.     short                errnum;
  165.     OSType                icondescriptor, trshdescriptor, finderSig = 'MACS';
  166.     ProcessSerialNumber    targetpsn;
  167.     AEDesc                targetaddress, propdesc, trshpropdesc, filedesc, directdesc, nulldesc, targetdesc, pointdesc;
  168.     AppleEvent            myae, myReply;
  169.     Rect                rect;
  170.     Boolean                fiddlewithA5;            // See below for the usage of the A5 variables
  171.     long                saveA5;
  172.  
  173.  
  174.     nulldesc.descriptorType = typeNull;
  175.     nulldesc.dataHandle = nil;
  176.  
  177.     icondescriptor = pBounds;
  178.     trshdescriptor = 'trsh';
  179.     if (GetFinderProcess(&targetpsn, sendToSelf) == noErr) 
  180.     {
  181.         if (usesystemmode)     TurnSystemModeOn();            // you really DON'T want to do this
  182.         fiddlewithA5 = IsRelativeProcessSerialNumber(&targetpsn);
  183.         if (fiddlewithA5)     saveA5 = SetCurrentA5();
  184.         
  185.         if ((errnum = AECreateDesc(typeProcessSerialNumber, &targetpsn, sizeof(ProcessSerialNumber), &targetaddress)) == noErr) {
  186.             if ((errnum = AECreateAppleEvent(kAECoreSuite, kAEGetData, &targetaddress, kAutoGenerateReturnID, kAnyTransactionID, &myae)) == noErr) {
  187.                 if ((errnum = AECreateDesc(typeType, &icondescriptor, sizeof(DescType), &propdesc)) == noErr) {
  188.                     if ((errnum = AECreateDesc(typeType, &trshdescriptor, sizeof(DescType), &trshpropdesc)) == noErr) {
  189.                         if ((errnum = CreateObjSpecifier(cProperty, &nulldesc, formPropertyID, &trshpropdesc, TRUE, &targetdesc)) == noErr) {
  190.                             if ((errnum = CreateObjSpecifier(cProperty, &targetdesc, formPropertyID, &propdesc, TRUE, &directdesc)) == noErr) {
  191.                                 if ((errnum = AEPutParamDesc(&myae, keyDirectObject, &directdesc)) == noErr) {
  192.                                     errnum = AESend(&myae, &myReply, sendmode, kAENeverInteract, kAEDefaultTimeout, nil, nil);
  193.                                     if ( errnum == noErr) {
  194.                                         // Get Rect
  195.                                         if (((errnum = AEGetParamDesc(&myReply, keyAEResult, cQDRectangle, &pointdesc)) == noErr))
  196.                                             rect = **((Rect **)(pointdesc.dataHandle));
  197.                                     }
  198.                                 }
  199.                                 
  200.                                 if (directdesc.dataHandle)    AEDisposeDesc(&directdesc);
  201.                             }
  202.                         }
  203.                         if (directdesc.dataHandle)    AEDisposeDesc(&trshpropdesc);
  204.                     }
  205.                     if (directdesc.dataHandle)    AEDisposeDesc(&propdesc);
  206.                 }
  207.                 if (directdesc.dataHandle)    AEDisposeDesc(&myae);
  208.             }
  209.             if (directdesc.dataHandle)    AEDisposeDesc(&targetaddress);
  210.         }
  211.         
  212.         // Get Rect
  213.         *r = rect;
  214.         
  215.         if (fiddlewithA5)     SetA5(saveA5);            // fix it up for crying out loud!
  216.         if (usesystemmode)     TurnSystemModeOff();            // we really DIDN'T want to do this
  217.     }
  218.     return(errnum);            // the result
  219. }